package com.zzedu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzedu.domain.ZzExerciseBook;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * 作业本信息Mapper接口
 *
 * @author Mr.Wang
 * @date 2025-06-02
 */
public interface ZzExerciseBookMapper extends BaseMapper<ZzExerciseBook> {

    /**
     * 获取提交次数
     *
     * @param unitId
     * @param stuId
     * @param interactionType 可为 null
     * @return
     */
    @Select({
            "<script>",
            "SELECT SUM(submit_count) FROM zz_exercise_book",
            "WHERE student_id = #{studentId}",
            "<if test='unitId != null'>",
            "AND node_id IN (",
            "SELECT id FROM zz_picture_node q",
            "WHERE q.p_node_id IN (",
            "SELECT id FROM zz_picture_node",
            "WHERE deleted = 0",
            "AND picture_book_id IN (",
            "SELECT id FROM zz_picture_book",
            "WHERE unit_id = #{unitId} AND deleted = 0",
            ")",
            "AND node_type = 3",
            ")",
            "AND q.deleted = 0 AND q.node_type = 2",
            ")",
            "</if>",
            "<if test='interactionType != null and interactionType != \"\"'>",
            "AND content_id IN (",
            "SELECT id FROM zz_subject_content",
            "WHERE deleted = 0 AND state = 0",
            "AND interaction = #{interactionType}",
            ")",
            "</if>",
            "</script>"
    })
    Long getSubmitCount(
            @Param("unitId") Long unitId,
            @Param("studentId") Long studentId,
            @Param("interactionType") String interactionType
    );

    /**
     * 获取视频时长
     *
     * @param unitId
     * @param stuId
     * @param subjectType 互动类型
     * @return
     */
    @Select({
            "<script>",
            "SELECT SUM(video_time) FROM zz_exercise_book",
            "WHERE student_id = #{studentId}",
            "<if test='unitId != null'>",
            "AND node_id IN (",
            "SELECT id FROM zz_picture_node q",
            "WHERE q.p_node_id IN (",
            "SELECT id FROM zz_picture_node",
            "WHERE deleted = 0",
            "AND picture_book_id IN (",
            "SELECT id FROM zz_picture_book",
            "WHERE unit_id = #{unitId} AND deleted = 0 ",
            ")",
            "AND node_type = 3",
            ")",
            "AND q.deleted = 0 AND q.node_type = 2",
            ")",
            "</if>",
            "<if test='subjectType != null and subjectType != \"\"'>",
            "AND content_id IN (",
            "SELECT id FROM zz_subject_content",
            "WHERE deleted = 0 AND state = 0",
            "AND subject_type = #{subjectType}",
            ")",
            "</if>",
            "</script>"
    })
    Long getVideoTimes(
            @Param("unitId") Long unitId,
            @Param("studentId") Long studentId,
            @Param("subjectType") String subjectType
    );

    @Select({
            "<script>",
            "SELECT ",
            "a.student_id AS stuId,",
            "SUM(IFNULL(a.video_time, 0) + IFNULL(a.remain_time, 0)) AS sumTimes,",
            "MAX(a.picture_book_id) AS lastBookId",
            "FROM zz_exercise_book a",
            "WHERE a.deleted = 0",
            "<if test='stuIdList != null and stuIdList.size() > 0'>",
            "AND a.student_id IN",
            "<foreach item='id' collection='stuIdList' open='(' separator=',' close=')'>",
            "#{id}",
            "</foreach>",
            "</if>",
            "GROUP BY a.student_id",
            "</script>"
    })
    List<Map<String, Object>> studentStudyInfoByStudentIdList(@Param("stuIdList") List<Long> stuIdList);
}
